/**
 * Created by nitin on 4/3/14.
 */
function FileUploadCtrl($scope, $fileUploader, $http) {
    'use strict';
    // create a uploader with options
    $scope.fileUploadId;
    var uploader = $scope.uploader = $fileUploader.create({
        scope: $scope,                          // to automatically update the html. Default: $rootScope
        url: BACK_END_BOM + 'upload.php?id=' + $scope.fileUploadId,
        queueLimit: 1
    });
    $scope.updateProduct = function () {
        $http.post(BACK_END_BOM + 'insert.php',
            {api: 2222})
            .success(function (data, status, headers, config) {
                console.info("Insert success data ", data);
                $scope.fileUploadId = data.id;
                uploader.uploadAll();
            }).error(function (data, status, headers, config) {
                console.info("data error ", data)
            });

    }
    // ADDING FILTERS
//    uploader.filters.push(function (item /*{File|HTMLInput}*/) {
//        console.info('filter1');
//        return true;
//    });
    // REGISTER HANDLERS
//    uploader.bind('afteraddingfile', function (event, item) {
//        console.info('After adding a file', item);
//    });
//    uploader.bind('whenaddingfilefailed', function (event, item) {
//        console.info('When adding a file failed', item);
//    });
//
//    uploader.bind('afteraddingall', function (event, items) {
//        console.info('After adding all files', items);
//    });
//
    uploader.bind('beforeupload', function (event, item) {
        console.info('Before upload', item);
    });
//
//    uploader.bind('progress', function (event, item, progress) {
//        console.info('Progress: ' + progress, item);
//    });
//
    uploader.bind('success', function (event, xhr, item, response) {
        console.info('Success', xhr, item, response);
    });
//
//    uploader.bind('cancel', function (event, xhr, item) {
//        console.info('Cancel', xhr, item);
//    });
//
//    uploader.bind('error', function (event, xhr, item, response) {
//        console.info('Error', xhr, item, response);
//    });
//
    uploader.bind('complete', function (event, xhr, item, response) {
        console.info('Complete', xhr, item, response);
    });
//
//    uploader.bind('progressall', function (event, progress) {
//        console.info('Total progress: ' + progress);
//    });
//
//    uploader.bind('completeall', function (event, items) {
//        console.info('Complete all', items);
//    });

    $scope.controller = {
        isImage: function (item) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    };

}